package com.net.oil.luckPan;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.car.MyCarDetailActivity;
import com.net.oil.http.AppHttpCode;
import com.net.oil.http.AppUrl;
import com.net.oil.http.custom.ApiException;
import com.net.oil.luckPan.bean.LotteryBean;
import com.net.oil.luckPan.bean.LuckPanBean;
import com.net.oil.luckPan.contract.LuckPanContract;
import com.net.oil.luckPan.presenter.LuckPanPresenter;
import com.net.oil.luckPan.view.LuckPanLayout;
import com.net.oil.luckPan.view.RotatePan;
import com.net.oil.util.glide.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LuckPanActivity extends BaseActivity<LuckPanPresenter> implements LuckPanLayout.AnimationEndListener, LuckPanContract.View {


    @BindView(R.id.rotate_pan)
    RotatePan rotatePan;
    @BindView(R.id.go)
    ImageView go;
    @BindView(R.id.luck_pan_layout)
    LuckPanLayout luckPanLayout;

    List<Bitmap> bitmaps;
    String[] strings = new String[6];
    @BindView(R.id.lottery_info_tv)
    TextView lotteryInfoTv;
    @BindView(R.id.action_rule_tv)
    TextView actionRuleTv;
    @BindView(R.id.luck_pan_title_img)
    ImageView luckPanTitleImg;

    private Context context;

    private List<LuckPanBean.PrizeListBean> prizeListBeans;
    private LuckPanBean.UserWinningPrizeRecordBean userWinningPrizeRecordBean;
    private LotteryBean lotteryBean;

    private int actionId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_luck_pan);
        ButterKnife.bind(this);
        context = this;

        actionId = getIntent().getIntExtra(Constant.INTENT_ACTION_ID_KEY, -1);

        presenter = new LuckPanPresenter(this);
        presenter.initLuckPan(actionId);
        luckPanLayout.setAnimationEndListener(this);
    }

    @OnClick(R.id.go)
    public void onViewClicked() {
        presenter.lottery(actionId);
    }

    @Override
    public void endAnimation(int position) {
        if (lotteryBean != null) {
            if (lotteryBean.getPrizeName() != null) {
                showDialog(lotteryBean.getPrizeName(), lotteryBean.getSurplusNumber());
                if (lotteryBean.getIswinning() == 1) {
                    /* Iswinnin=1 抽中奖品 */
                    lotteryInfoTv.setText("恭喜您抽中：" + lotteryBean.getPrizeName());
                }
            }
        }


    }

    /**
     * 初始化抽奖转盘成功
     *
     * @param luckPanBean 抽奖初始化数据
     */
    @Override
    public void initLuckPanSuccess(LuckPanBean luckPanBean) {
        if (luckPanBean != null) {
            /* 奖品信息 */
            prizeListBeans = luckPanBean.getPrizeList();
            if (prizeListBeans != null && prizeListBeans.size() == 6) {
                for (int i = 0; i < prizeListBeans.size(); i++) {
                    strings[i] = prizeListBeans.get(i).getName();
                }
            }
            rotatePan.setStr(strings);

            if (!TextUtils.isEmpty(luckPanBean.getLotteryRules())) {
                actionRuleTv.setText(luckPanBean.getLotteryRules());
            }

            if (!TextUtils.isEmpty(luckPanBean.getLotteryImage())) {
                GlideUtil.getInstance().loadImage(this, luckPanTitleImg, AppUrl.getImageUrl(luckPanBean.getLotteryImage()), R.drawable.luck_pan_img_err);
            }

            /* 中奖信息 */
            userWinningPrizeRecordBean = luckPanBean.getUserWinningPrizeRecord();
            if (userWinningPrizeRecordBean != null) {
                if (!TextUtils.isEmpty(userWinningPrizeRecordBean.getPrizeName())) {
                    lotteryInfoTv.setText("恭喜您抽中：" + userWinningPrizeRecordBean.getPrizeName());
                } else {
                    lotteryInfoTv.setText("您还未中奖请点击抽奖");
                }
            } else {
                lotteryInfoTv.setText("您还未中奖请点击抽奖");
            }
        }
    }

    /**
     * 初始化抽奖转盘失败
     */
    @Override
    public void initLuckPanFailed() {

    }

    /**
     * 抽奖成功
     *
     * @param lotteryBean 抽奖结果
     */
    @Override
    public void lotterySuccess(LotteryBean lotteryBean) {
        this.lotteryBean = lotteryBean;
        if (lotteryBean != null && prizeListBeans != null) {
            for (int i = 0; i < prizeListBeans.size(); i++) {
                if (lotteryBean.getPrizeId() == prizeListBeans.get(i).getPrizeid()) {
                    luckPanLayout.rotate(i, 100);
                }
            }
        }
    }

    /**
     * 抽奖失败
     */
    @Override
    public void lotteryFailed(ApiException e) {
        if (e.getErrorCode() == AppHttpCode.NO_CAR_INFO || e.getErrorCode() == AppHttpCode.NO_CAR_NUMBER){
            if (!TextUtils.isEmpty(e.getErrorMessage())){
                showErrDialog(e.getErrorMessage());
            }else {
                String message = "";
                if (e.getErrorCode() == AppHttpCode.NO_CAR_INFO){
                    message = "请先前往爱车管理,添加爱车信息并完善车牌号信息";
                }else if (e.getErrorCode() == AppHttpCode.NO_CAR_NUMBER){
                    message = "请先完善用户爱车的车牌号";
                }
                showErrDialog(message);
            }
        }
    }

    /**
     * 提示中奖信息
     *
     * @param lottery 奖品
     * @param count   剩余抽奖次数
     */
    private void showDialog(String lottery, int count) {
        String message;
        if (count == 9999) {
            message = "抽中奖品：" + lottery;
        } else {
            message = "抽中奖品：" + lottery + "\n剩余抽奖次数：" + count + "次";
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage(message);
        builder.setPositiveButton("确定", null);
        builder.show();
    }

    /**
     * 根据接口返回的code，如果code 为703或者702，则跳转到爱车详情
     *
     * @param message 提示信息（从接口message中取）
     */
    private void showErrDialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage(message);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(context, MyCarDetailActivity.class));
            }
        });
        builder.show();
    }
}
